package com.atguigu.utils;

import org.wltea.analyzer.core.IKSegmenter;
import org.wltea.analyzer.core.Lexeme;

import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;

//切分单词工具类
public class MyKeyWordUtil {
    public static List<String> splitKeyWord(String keyword) throws IOException {
        //todo 创建集合用于存放切分后的词
        ArrayList<String> words = new ArrayList<>();

        //todo 创建IK分词对象 true：一个词只能出现一次，false：只要是词就拆开
        IKSegmenter ikSegmenter = new IKSegmenter(new StringReader(keyword), false);

        //todo 循环取出切分后的词放入集合
        Lexeme lexeme = ikSegmenter.next();//第一条数据对象
        while (lexeme!=null){
            String word = lexeme.getLexemeText();
            words.add(word);
            lexeme=ikSegmenter.next();
        }

        return words;




    }
}

